/*
 * Copyright 2011 Dominik Pretzsch <dominik.pretzsch at gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.blacksheep.jmoteextract.remote;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import de.blacksheep.jmoteextract.progress.ProgressManager;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Dominik Pretzsch <dominik.pretzsch at gmail.com>
 */
public class RemoteProgressObserver extends UnicastRemoteObject 
                                    implements IRemoteObserver {
    
    private static final int REMOTE_SET_BYTESDONE  = 0;
    private static final int REMOTE_SET_BYTESTOTAL = 1;
    private static final int REMOTE_SET_ERRORMSG   = 2;
    
    private int hash;
    private ProgressManager progress;
    
    private static final Logger LOGGER =  Logger.getLogger(RemoteProgressObserver.class.getName());
    
    public RemoteProgressObserver(int hash) throws RemoteException {
        super();
        this.hash = hash;
        this.progress = ProgressManager.getInstance();
    }

    public void update(int id, Object data) throws RemoteException {
        switch (id) {
            case REMOTE_SET_BYTESDONE:
                LOGGER.log(Level.FINE, "Setting {0} Bytes done for {1}", new Object[]{String.valueOf((Long) data), hash});
                progress.setBytesDone(hash, (Long) data);
                break;
            case REMOTE_SET_BYTESTOTAL:
                LOGGER.log(Level.FINE, "Setting {0} Bytes in total for {1}", new Object[]{String.valueOf((Long) data), hash});
                progress.setBytesTotal(hash, (Long) data);
                break;
            case REMOTE_SET_ERRORMSG:
                LOGGER.log(Level.FINE, "Setting '{0}' as error message for {1}", new Object[]{String.valueOf(data), hash});
                progress.setErrorMsg(hash, (String) data);
                break;
        }
    }   
}
